package middleware

import (
	"github.com/gin-gonic/gin"
	"shopstar/global"
	"shopstar/reponse"
	"shopstar/util"
)

// JwtAuth 获取是请求-》body、header、cookie
func JwtAuth() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		token := ctx.Request.Header.Get("x-token")

		if token == "" {
			reponse.FailWithErr(global.ErrJwtAuthIllegal, ctx)
			ctx.Abort()
			return
		}

		if !util.JWT.Expired(token, global.JwtKey) {
			reponse.FailWithErr(global.ErrJwtAuthExpired, ctx)
			ctx.Abort()
			return
		}

		ctx.Next()
	}
}
